//
//  MineProfileController.m
//  Mine
//
//  Created by CPX on 2018/9/6.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MineProfileController.h"
#import "MineTool.h"

/**
 我的详情
 @author yzl
 */
@interface MineProfileController()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UIButton *avatarImage;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) UITextField *nameTF;

@end

@implementation MineProfileController

- (void)viewDidLoad{
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}

- (void)initViews{
    
    self.view.backgroundColor = UIColorHex(#F8F8F8);
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor= [UIColor whiteColor];
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 5;
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NavHeight+12);
        make.leading.mas_equalTo(12);
        make.height.mas_equalTo(294);
        make.trailing.mas_equalTo(-12);
    }];
    
    UIView *cellMinePhoto = [self createCellMinePhoto];
    [view addSubview:cellMinePhoto];
    [cellMinePhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(12);
        make.top.mas_equalTo(0);
        make.trailing.mas_equalTo(-12);
        make.height.mas_equalTo(56);
    }];
    
    UIView *cellMineName = [self createCellMineName];
    [view addSubview:cellMineName];
    [cellMineName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(12);
        make.top.mas_equalTo(cellMinePhoto.mas_bottom);
        make.trailing.mas_equalTo(-12);
        make.height.mas_equalTo(56);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = UIColorHex(#EFEFEF);
    [button setTitleColor:UIColorHex(#525252) forState:UIControlStateNormal];
    [button setTitle:TLString(@"Sign_Out") forState:UIControlStateNormal];
    [button addTarget:self action:@selector(onSignoutTouched) forControlEvents:UIControlEventTouchUpInside];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 22;
    [view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-43);
        make.width.mas_equalTo(280);
        make.centerX.mas_equalTo(view);
        make.height.mas_equalTo(44);
    }];
}

- (void)initNavigation{
    [self initDefaultNavigation];
    [self.myNavigationBar.backButton setTitle:TLString(@"person_infor") forState:UIControlStateNormal];
}

- (void)loadData{
    [self.avatarImage sd_setImageWithURL:[NSURL URLWithString:[MineTool sharedTool].avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"no_photo"]];
    
    self.nameTF.text = [MineTool sharedTool].username;
}

- (BOOL)gestureRecognizerShouldBegin{
    return NO;
}

- (void)onLeftButtonTouched{
    if (self.image || ![self.nameTF.text isEqualToString:[MineTool sharedTool].username]) {
        [self showLoadingInView];
        [MineApi updateUsername:self.nameTF.text portraint:self.image success:^(id response) {
            [self hideHUDInView];
            [MineTool sharedTool].userToken = response[@"token"];
            [MineTool sharedTool].username = response[@"username"];
            [MineTool sharedTool].email = response[@"email"];
            [MineTool sharedTool].avatar = response[@"avatar"];
            [MineTool sharedTool].isLogin = YES;
            [[MineTool sharedTool] saveUserInfo];
            [super onLeftButtonTouched];
        } failure:^(NSError *error) {
            [MBProgressTool showTextForView:self.view text:error.userInfo[@"msg"]];
        }];
    }
    else{
        [super onLeftButtonTouched];
    }
}

#pragma mark - 分开创建

- (UIView *)createCellMinePhoto{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-24, 56)];
    UILabel *l1 = [self createLabelWith:TLString(@"Photo") font:[UIFont systemFontOfSize:16]];
    [view addSubview:l1];
    [l1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(12);
        make.centerY.mas_equalTo(view);
        make.height.mas_equalTo(20);
        make.width.mas_greaterThanOrEqualTo(10);
    }];
    
    UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
    [img addTarget:self action:@selector(onAvtarTouched) forControlEvents:UIControlEventTouchUpInside];
//    [img setImage:[UIImage imageNamed:@"no_photo"] forState:UIControlStateNormal];
    img.layer.masksToBounds = YES;
    img.layer.cornerRadius = 17;
    [view addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-12);
        make.width.height.mas_equalTo(34);
        make.centerY.mas_equalTo(view);
    }];
    self.avatarImage = img;
    
    UIView *line = [UIView new];
    line.backgroundColor = UIColorHex(#EDEDED);
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(12);
        make.trailing.mas_equalTo(-12);
        make.height.mas_equalTo(.5);
        make.bottom.mas_equalTo(-.5);
    }];
    
    return view;
}

- (UIView*)createCellMineName{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-24, 56)];
    UILabel *l1 = [self createLabelWith:TLString(@"Nickname") font:[UIFont systemFontOfSize:16]];
    [view addSubview:l1];
    [l1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(12);
        make.centerY.mas_equalTo(view);
        make.height.mas_equalTo(20);
        make.width.mas_greaterThanOrEqualTo(10);
    }];
    
    self.nameTF = [[UITextField alloc] init];
    self.nameTF.font = [UIFont systemFontOfSize:16];
    self.nameTF.textColor = UIColorHex(#000A05);
    [view addSubview:self.nameTF];
    [self.nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-12);
        make.leading.mas_equalTo(l1.mas_trailing).offset(12);
        make.centerY.mas_equalTo(view);
        make.height.mas_equalTo(20);
    }];
    
    UIView *line = [UIView new];
    line.backgroundColor = UIColorHex(#EDEDED);
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(12);
        make.trailing.mas_equalTo(-12);
        make.height.mas_equalTo(.5);
        make.bottom.mas_equalTo(-.5);
    }];
    
    return view;
}

- (UILabel*)createLabelWith:(NSString*)text font:(UIFont*)font{
    UILabel *l = [UILabel new];
    l.textColor = UIColorHex(#525252);
    l.text = text;
    l.font = font;
    return l;
}

#pragma mark - textfield


#pragma mark - picker

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"info");
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    image = [image imageByResizeToSize:CGSizeMake(200, 200)];
    
    self.image = image;
    [self.avatarImage sd_cancelImageLoadForState:UIControlStateNormal];
    [self.avatarImage setImage:image forState:UIControlStateNormal];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)open:(NSInteger)index{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    if(true) {
        imagePickerController.delegate = self; //设置代理
        imagePickerController.allowsEditing = YES;
        if (index == 0) {
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; //图片来源
        }else if (index == 1) {
            
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}


#pragma mark - actions

- (void)onSignoutTouched{
    MyAlertViewV3 *a = [MyAlertViewV3 alertViewWithTitle:TLString(@"log_out_deter") block:^(NSInteger btnIdx) {
        if (btnIdx == 1) {
            //1是确定
            [[MineTool sharedTool] clearUserInfo];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
    [a addActionWithTitle:TLString(@"social_cancel") color:UIColorHex(#334139)];
    [a addActionWithTitle:TLString(@"YES") color:UIColorHex(#00732F)];
    [a show:NO];
}

- (void)onAvtarTouched{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Choose how to get pictures" message:@"" preferredStyle: UIAlertControllerStyleActionSheet];
    UIAlertAction *a1 = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self open:0];
    }];
    UIAlertAction *a2 = [UIAlertAction actionWithTitle:@"Album" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self open:1];
    }];
    UIAlertAction *a3 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:a1];
    [alertController addAction:a2];
    [alertController addAction:a3];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
